package com.example.imgrecognition.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.example.imgrecognition.R;
import com.zk.code.net.NetConfig;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

import io.netty.buffer.ByteBuf;
import io.netty.channel.socket.DatagramPacket;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.internal.operators.observable.ObservableSubscribeOn;


public class TestRXActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_rx);
        create();
        just();
        delay();
        count();
        sendInterval();
    }

    private void just(){
        Observable.just("我要好好学习").subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Log.e("str-->",s);
            }
        });
    }

    private void delay(){
        Observable.just("111").delay(2, TimeUnit.SECONDS).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Log.e("delay-->",s);
            }
        });
    }

    /**
     * 产生一个倒计时的 Observable
     * @param time
     * @return
     */

    public Observable<Long> countdown(final long time) {
        return Observable.interval(1, TimeUnit.SECONDS)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(@NonNull Long aLong) throws Exception {
                        return time - aLong;
                    }
                }).take( time + 1 );
    }

    private void sendInterval(){
        Observable.interval(2,2, TimeUnit.SECONDS)
                .take(5)
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long value) {
                        Log.e("发起报文结束指令","发起"+value);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    //如果你需要一个一段时间之后才发射的Observable，你可以像下面的例子使用timer()：
    private void timer(){
        Observable.timer(3,TimeUnit.SECONDS)
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

//它将3秒后发射0,然后就完成了。


    private void count(){
        countdown(10).subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                Log.e("zhao", "accept: 倒计时： " + aLong);
            }
        });
    }

    private void create(){
        //ObservableSubscribeOn<String> ob = new ObservableSubscribeOn<>()
        //被观察者
        Observable<String> stringObServable = Observable.create(new ObservableOnSubscribe<String>() {

            @Override
            public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Exception {
                emitter.onNext("hello");
                emitter.onNext("wolrd");
                emitter.onComplete();
            }
        });
        //观察者
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            @Override
            public void onNext(@NonNull String s) {
                Log.e("接收到的值",s);
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {
                Log.e("接收到","完成了");
            }
        };
        //订阅
        stringObServable.subscribe(observer);
    }
}
